package com.hotel.servlet;

import com.hotel.bean.*;
import com.hotel.dao.CollectionDao;
import com.hotel.dao.OrderDao;
import com.hotel.util.DBOperation;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author KungSu.
 * @create 2021-05-13 9:27
 */
@WebServlet(value = "/CollectionHotelServlet")
public class CollectionHotelServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Account account = (Account) request.getSession().getAttribute("curUser");
        SqlSession sqlSession = DBOperation.getSqlSession();
        //新建实体类
        CollectionDao collectionDao =  sqlSession.getMapper(CollectionDao.class);
        String type = request.getParameter("type");
        switch (type){
            case "add":
                Collection_Hotel collectionHotel = new Collection_Hotel();
                collectionHotel.setAccount_id(account.getAccount_id());
                String hotel_name = request.getParameter("hotel");
                collectionHotel.setHotel_name(hotel_name);
                collectionDao.saveHotel(collectionHotel);
                sqlSession.close();
                request.getRequestDispatcher("/jsp/"+hotel_name+"Hotel.jsp").forward(request,response);
                break;
            case "delete":
                String hotel = request.getParameter("hotel");
                break;
            case "find":
                List<Collection_Hotel> collection_hotels = collectionDao.findAll(account.getAccount_id());
                for (Collection_Hotel collectionHotel1 :collection_hotels){
                    System.out.println(collectionHotel1);
                }
                sqlSession.close();
                request.setAttribute("collectionHotel",collection_hotels);
                request.getRequestDispatcher("/jsp/wodeshoucangjiudian.jsp").forward(request,response);
                break;
        }



    }
}
